package org.ourgrid.broker.commands.executors;

import org.ourgrid.broker.commands.SchedulerData;
import org.ourgrid.broker.util.UtilConverter;
import org.ourgrid.common.interfaces.to.JobEndedInterested;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import br.edu.ufcg.lsd.commune.container.servicemanager.ServiceManager;
import br.edu.ufcg.lsd.commune.identification.DeploymentID;

public class JobEndedExecutor extends SchedulerCommandExecutor {
	
	private int jobID;
	private int state;
	private String interestedID;
	
	/**
	 * <JOB_ENDED jobID='int' state='int' interestedID='String'/>
	 */
	public void execute(SchedulerData data, ServiceManager manager) {
		
		fillData(data.getXml());
		
		JobEndedInterested interested = manager.createStub(new DeploymentID(interestedID), 
				JobEndedInterested.class);
		interested.jobEnded(jobID, UtilConverter.getJobState(state));
	}
	
	private void fillData(String xml) {
		
		Document logDocFile = super.getXMl(xml);
		
		Element element = logDocFile.getDocumentElement();
		
		jobID = Integer.valueOf(element.getAttribute("jobID"));
		state = Integer.valueOf(element.getAttribute("state"));
		interestedID = element.getAttribute("interestedID");
	}
}
